﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CayleyTree
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private Graphics graphics;
        double th1 = 30 * Math.PI / 180;   //右分支角度
        double th2 = 20 * Math.PI / 180;   //左分支角度
        double per1 = 0.6;  //右分支长度比
        double per2 = 0.7;  //左分支长度比
        int n = 10; //递归深度
        double leng = 100; //主干长度
        Pen mypen = new Pen(Color.Blue);
        private void button1_Click(object sender, EventArgs e)
        {
            if (graphics == null) graphics = this.CreateGraphics();
            drawCayleyTree(n, 250, 460, leng, -Math.PI / 2);
        }

       

        void drawCayleyTree(int n,
                double x0, double y0, double leng, double th)

            //x0,y0为位置吧
            //依次为递归深度n，主干长度leng
        {
            if (n == 0) return;

            double x1 = x0 + leng * Math.Cos(th);
            double y1 = y0 + leng * Math.Sin(th);

            drawLine(x0, y0, x1, y1);

            drawCayleyTree(n - 1, x1, y1, per1 * leng, th + th1);
            drawCayleyTree(n - 1, x1, y1, per2 * leng, th - th2);
        }
        void drawLine(double x0, double y0, double x1, double y1)
        {
            graphics.DrawLine(
                mypen,
                (int)x0, (int)y0, (int)x1, (int)y1);
        }

       

        private void button2_Click(object sender, EventArgs e)
        {
            try{
                int n1 = n;
                double leng1=leng;
                double per11=per1;
                double per21=per2;
                double th11 = th1;
                double th21 = th2;



                if (textBox1.Text != "")
                {
                    n1 = int.Parse(textBox1.Text);
                }
                if (textBox2.Text != "")
                {
                    
                    leng1 = double.Parse(textBox2.Text);
                }
                if (textBox3.Text != "")
                {
                    
                    per11 = double.Parse(textBox3.Text);
                }
                if (textBox4.Text != "")
                {
                    
                    per21 = double.Parse(textBox4.Text);
                }
                if (textBox5.Text != "")
                {
                    th11 = double.Parse(textBox5.Text) * Math.PI / 180;
                }
                if (textBox6.Text != "")
                {
                    th21 = double.Parse(textBox6.Text) * Math.PI / 180;
                }

                if (n1 >= 0&&per11<=1&&per11>0 && per21 <= 1 && per21 > 0)
                {
                    this.th1 = th11;
                    this.th2 = th21;
                    this.per1 = per11;
                    this.per2 = per21;
                    this.n = n1;
                    this.leng = leng1;
                    MessageBox.Show("修改成功");
                }
                else
                {
                    MessageBox.Show("输入不合法,异常1");
                }


            }
            catch (Exception )
            {
                MessageBox.Show("输入不合法,异常2");
                //阻止修改
                 this.th1 = 30 * Math.PI / 180;   //右分支角度
                 this.th2 = 20 * Math.PI / 180;   //左分支角度
                 this.per1 = 0.6;  //右分支长度比
                 this.per2 = 0.7;  //左分支长度比
                 this.n = 10; //递归深度
                 this.leng = 100; //主干长度
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            colorDialog1.ShowDialog();
            this.mypen.Color = colorDialog1.Color;
        }
    }
}
